package com.xigua.字符串;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/3/219:52
 */
public class _14_最长公共前缀 {

    public static void main(String[] args) {
        longestCommonPrefix(new String[]{"flower","flow","flight"});
    }

    public static String longestCommonPrefix(String[] strs) {
        if (strs == null) return "";
        Arrays.sort(strs);
        StringBuffer stringBuffer = new StringBuffer();
        String str = strs[0];
        String end = strs[strs.length - 1];
        for (int i = 0; i < Math.min(str.length(),end.length()); i++) {
            if (str.charAt(i) == end.charAt(i)){
                stringBuffer.append(str.charAt(i));
            }else {
                return stringBuffer.toString();
            }
        }
        return stringBuffer.toString();
    }

}
